$("#Switchbox", window.parent.document).on("click", function () {
    ModeSwitch.modeSwitch();
    commonbgcolor();
    markbgcolor()
})

$("#notes", window.parent.document).on("click", function () {
    getTags('index.html');
    iframeOn('notes');
})

//打开笔记
function opennote(url) {
    $('#tagList').hide();
    $('#markdown').show();
    document.getElementById('markdown').contentWindow.opennote(url);
}
//关闭笔记
function closenote() {
    $('#tagList').show();
    $('#markdown').hide();
}

//截取所有笔记链接
function getTags(url) {
    closenote();
    var tagList = '';
    $.ajax({
        sync: false,
        type: 'GET',
        dataType: 'json',
        url: 'https://gitee.com/api/v5/repos/'+giteeID+'/mweb/contents/' + url,
        success: function (result) {

            var data = utf8to16(window.atob(result.content));
            if (/<[\s]*nav/.test(data))
                data = data.substring(0, data.lastIndexOf('<nav'));

            //获取笔记数量
            var lengthRegExp = /<[\s]*div[\s]+class[\s]*=[\s]*"content"[\s]*>/g;
            var notelength = data.match(lengthRegExp).length;

            //截取文件名
            var hrefRegExp = /<[\s]*div[\s]+class[\s]*=[\s]*"content"[\s]*>[\s]*<[\s]*a[\s]+href[\s]*=[\s]*"[%A-Za-z0-9]+[\.]html[\s]*"[\s]*[\s]*>/g;
            //截取笔记名
            var noteRegExp = /<[\s]*h2[\s]+class[\s]*=[\s]*"title"[\s]*>.+<[\s]*[\/][\s]*h2[\s]*>/g;
            //截取分类
            var categoryRegExp = /<a[\s]+href[\s]*=[\s]*'[%A-Za-z0-9]+.html'[\s]*>.+<[\s]*[\/]a[\s]*>/g;
            //截取时间
            var dataRegExp = /<[\s]*span[\s]+class[\s]*=[\s]*"date"[\s]*>[\s]*[0-9]{4}[\/][0-9]{2}[\/][\s0-9]+:[0-9]+[\s]+[\u4e00-\u9fa5]{2}[\s]*<[\s]*[\/][\s]*span[\s]*>/g;


            for (var n = 0; n < notelength; n++) {
                var href = data.match(hrefRegExp)[n];
                var note = data.match(noteRegExp)[n];
                var category = data.match(categoryRegExp)[n];
                var datatime = data.match(dataRegExp)[n];

                tagList += `
                    <li class="ModeBG">
                        <div class="notemsg_1">
                            <p class="notename">${note.substring(note.indexOf('>') + 1, note.lastIndexOf('<'))}</p>
                            <p class="notetag">${category.substring(category.indexOf('>') + 1, category.lastIndexOf('<'))}</p>
                        </div>
                        <div class="notemsg_2">
                            <a class="toread" onclick="opennote('${href.substring(href.indexOf('href="') + 6, href.lastIndexOf('"'))}')">前往阅读</a>
                            <p class="notetime">发布于 ${datatime.substring(datatime.indexOf('>') + 1, datatime.lastIndexOf('<'))}</p>
                        </div>
                    </li>
                `
            };
            document.getElementById('tagList').innerHTML = tagList;
            iframeOn('notes');
        }
    })
}